package com.kimo.shop.admin.controller;


import cn.hutool.core.date.DateUtil;
import com.kimo.shop.bean.app.dto.OrderCountData;
import com.kimo.shop.bean.param.OrderPayParam;
import com.kimo.shop.bean.param.StatisticsRefundParam;
import com.kimo.shop.security.admin.util.SecurityUtils;
import com.kimo.shop.service.OrderRefundService;
import com.kimo.shop.service.OrderService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author kimo-taozhouchuan on 2019/10/17.
 */
@RestController
@RequestMapping("/admin/statisticsOrder")
@AllArgsConstructor
public class StatisticsOrderController {

    private final OrderService orderService;
    private final OrderRefundService orderRefundService;

    @GetMapping("/orderCount")
    @ApiOperation(value = "查询店铺订单各状态数量", notes = "查询店铺订单各状态数量")
    public ResponseEntity<OrderCountData> getOrderCount() {
        OrderCountData orderCountData = orderService.getOrderCountOfStatusByShopId(SecurityUtils.getSysUser().getShopId());
        return ResponseEntity.ok(orderCountData);
    }


    @GetMapping("/orderPayByShopId")
    @ApiOperation(value = "通过时间获取支付信息")
    public ResponseEntity<OrderPayParam> orderPayByShopId() {
        OrderPayParam actualTotal = orderService.getPayUserCountByshopId(
                SecurityUtils.getSysUser().getShopId(),DateUtil.beginOfDay(DateUtil.date()), DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(actualTotal);
    }

    @GetMapping("/getActualTotalByHour")
    @ApiOperation(value = "通过24小时分段获取支付金额")
    public ResponseEntity<OrderPayParam> getActualTotalByHour() {
        OrderPayParam payList = orderService.getActualTotalByHour(
                SecurityUtils.getSysUser().getShopId(),DateUtil.beginOfDay(DateUtil.date()),DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(payList);
    }


//    private void lalala() {
//        List<OrderRefund> list = orderRefundService.list(null);
//        for (OrderRefund orderRefund : list) {
//            BuyerReasonType buyerReasonType = BuyerReasonType.instance(orderRefund.getBuyerReason());
//            if(Objects.nonNull(buyerReasonType)){
//                orderRefund.setBuyerReason(buyerReasonType.value().toString());
//            }
//        }
//        orderRefundService.updateBatchById(list);
//    }

    @GetMapping("/getActualTotalByDay")
    @ApiOperation(value = "通过天数分段获取支付金额")
    public ResponseEntity<List<OrderPayParam>> getActualTotalByDay() {
        List<OrderPayParam> payList = orderService.getActualTotalByDay(
                SecurityUtils.getSysUser().getShopId(),DateUtil.endOfDay(DateUtil.lastMonth()),DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(payList);
    }

    @GetMapping("/getOrderRefundByTime")
    @ApiOperation(value = "通过时间获取比率信息")
    public ResponseEntity<StatisticsRefundParam> getOrderRefundByTime() {
        StatisticsRefundParam refundParam = orderRefundService.getOrderRefundByShopId(
                SecurityUtils.getSysUser().getShopId(),DateUtil.beginOfDay(DateUtil.date()), DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(refundParam);
    }

    @GetMapping("/getOrderRefundDayByTime")
    @ApiOperation(value = "通过时间获取分段比率信息及退款金额信息")
    public ResponseEntity<List<StatisticsRefundParam>> getOrderRefundById() {
        List<StatisticsRefundParam> refundList = orderRefundService.getOrderRefundByShopIdAndDay(
                SecurityUtils.getSysUser().getShopId(),DateUtil.endOfDay(DateUtil.lastMonth()),DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(refundList);
    }

    @GetMapping("/getRefundRankingByProd")
    @ApiOperation(value = "根据商品名生成退款排行")
    public ResponseEntity<List<StatisticsRefundParam>> getRefundRankingByProd() {
        List<StatisticsRefundParam> refundList = orderRefundService.getRefundRankingByProd(
                SecurityUtils.getSysUser().getShopId(),DateUtil.endOfDay(DateUtil.lastMonth()),DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(refundList);
    }

    @GetMapping("/getRefundRankingByReason")
    @ApiOperation(value = "根据退款原因生成退款排行")
    public ResponseEntity<List<StatisticsRefundParam>> getRefundRankingByReason() {
        List<StatisticsRefundParam> refundList = orderRefundService.getRefundRankingByReason(
                SecurityUtils.getSysUser().getShopId(),DateUtil.endOfDay(DateUtil.lastMonth()),DateUtil.endOfDay(DateUtil.date()));
        return ResponseEntity.ok(refundList);
    }

}
